
from flask_restful import Resource, reqparse
from controllers.console import api
from core.roles.role_check import check_perms
from libs.login import login_required
# from tasks.robot.link_func_group_task import link_func_group_task
from services.robot.intent_build_service import IntentBuildService
# class IntentRecGroupApi(Resource):

    # @login_required
    # @check_role(base_role=AccountRole.D_TENANT_ADMIN)
    # @marshal_with(intent_rec_group_fields)
    # def post(self):
    #     if not isinstance(current_user,Account):
    #         raise NotLoginError()
    #     argparser = reqparse.RequestParser()
    #     argparser.add_argument('name', type=str, required=True, location='json')
    #     argparser.add_argument('descrip', type=str, required=False, location='json')
    #     argparser.add_argument('doc_language', type=str, required=False,default="Chinese Simplified", location='json')
    #     args = argparser.parse_args()
    #     name = args['name']
    #     descrip = args.get('descrip')
    #     doc_lang = args.get('doc_language',"Chinese Simplified")
    #     return IntentRecGroupService.create_group(current_user,name,descrip,doc_lang)
    
    # @login_required
    # @check_role(base_role=AccountRole.D_TENANT_ADMIN)
    # @marshal_with(intent_rec_group_pagenation_fields)
    # def get(self):
    #     if not isinstance(current_user,Account):
    #         raise BadRequest("用户信息错误")
    #     argparser  = reqparse.RequestParser()
    #     argparser.add_argument("page",type=int,default=-1,location="args")
    #     argparser.add_argument("page_size",type=int,default=-1,location="args")
    #     argparser.add_argument("name",type=str,required=False,default=None,location="args")
    #     args = argparser.parse_args()
    #     page = args.get("page",-1)
    #     page_size = args.get("page_size",-1)
    #     name = args.get("name",None)
    #     return IntentRecGroupService.get_group_list(current_user,page,page_size,name)

class IntentRecGroupApi(Resource):
    pass
    # TODO 添加意图组
    
class LinkTermToGroupApi(Resource):
    @login_required
    # @check_role(base_role=AccountRole.TENANT_ADMIN)
    @check_perms(perms=['make_ds:link'])
    def post(self):
        argparser = reqparse.RequestParser()
        argparser.add_argument("group_id", type=str, required=True,location="json")
        argparser.add_argument("func_ids", type=str,action='append', required=True,location="json")
        argparser.add_argument("all_terms", type=bool, required=False,defalult=False,location="json")
        args = argparser.parse_args()
        group_id = args.get("group_id","")
        func_ids = args.get("func_ids",[])
        all_terms = args.get("all_terms",False)
        if IntentBuildService.has_mission(group_id):
            return {"msg": "任务已存在，请勿重复提交！"},400
        success,msg = IntentBuildService.link_funcs_to_group(group_id,func_ids,all_terms)
        if success:
            return {"code":200,"msg": msg},200
        else:
            return {"code":400,"msg": msg},400
    
    @login_required
    # @check_role(base_role=AccountRole.TENANT_ADMIN)
    @check_perms(perms=['make_ds:link'])
    def get(self):
        argparser = reqparse.RequestParser()
        argparser.add_argument("group_id", type=str, required=True,location="args")
        args = argparser.parse_args()
        group_id = args.get("group_id","")
        if not group_id:
            return {"msg": "参数错误！"},400
        return IntentBuildService.link_terms_to_group_task_status(group_id)

api.add_resource(LinkTermToGroupApi,'/rob/func/group/link')